<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/9/29
 * Time: 11:03
 */

namespace app\index\controller;
use app\index\model\Goods as GoodsModel;
use think\Build;
use think\Db;
use think\Model;
use think\Request;
use think\Url;

class Goods
{
    /**
     * @param Request $request
     * @return array
     * 商品列表
     */
    public function index(Request $request){
        $param = $request->param();
        $order = ['id' => 'desc'];
        if(!empty($param['order'])) $order = [$param['order'] => 'asc'];
        $condition['state'] = ['eq',1];
        if(!empty($param['low_price'])) $condition['price'] = ['egt',$param['low_price']];
        if(!empty($param['high_price'])) $condition['price'] = ['elt',$param['high_price']];
        if(!empty($param['goods_name'])) $condition['title'] = ['like','%'.$param['goods_name'].'%'];
        if(!empty($param['type_id'])) $condition['goods_id'] = ['eq',$param['type_id']];
        $data = Db::name('shop_goods')
            ->field('id,title,price,picUrl,sale_number')
            ->where($condition)->order($order)

            ->paginate(7);
        return ['code' => 200,'message' => '商品列表','data' => $data];
    }

    /**
     * @param $id
     * @return array
     * 商品详情
     */
    public function view($member_id = 0,$id,$longitude = '127.2216796875',$latitude =' 45.5525252513',$shop_id = 0)
    {
        $public_condition['auding_status'] = ['eq',2];
        $info = Db::name('shop_goods')
            ->alias('g')
            ->join('shop_spec_attr a','g.id = a.goods_id')
            ->join('shop s','a.shop_id = s.id')
            ->field('g.id,g.title,g.price,g.market_price,g.picArr,a.inventory,s.shop_name,s.shop_address,s.shop_phone')
            ->select($id);
        $info = Db::name('shop_goods')
            ->alias('g')
            ->field('g.id,g.title,g.price,g.market_price,g.picArr,g.content,g.buy_must_know')
            ->join('shop_spec_attr a','g.id = a.goods_id')
            ->find($id);
        $info['picArr'] = unserialize($info['picArr']) ? unserialize($info['picArr']) : [];
        $list = Db::name('shop_spec_attr')
            ->alias('a')
            ->where('a.goods_id',$id)
            ->field('a.goods_id,a.shop_id,s.shop_title shop_name,s.shop_address,s.shop_phone,s.thumbnail pic_url,s.longitude,s.latitude')
            ->join('shop s','a.shop_id = s.id')
            ->group('shop_id')
            ->select()->toArray();
        if(!empty($list)){
            foreach ($list as $k => &$v){
                $v['inventory'] = Db::name('shop_spec_attr')->where('shop_id',$v['shop_id'])->where('goods_id',$v['goods_id'])->sum('inventory');
            }
        }
        if($shop_id == 0) $shop_id = $list[0]['shop_id'];
        $info['inventory'] = Db::name('shop_spec_attr')->where('goods_id',$id)->where('shop_id',$shop_id)->sum('inventory');
        $info['list'] = $list;
        $info['content_url'] = Request::instance()->domain().Url::build('web',['id' => $id,'field' => 'content']);
        $info['buy_must_know_url'] = Request::instance()->domain().Url::build('web',['id' => $id,'field' => 'buy_must_know']);
        foreach ($info['list'] as $k => &$v){
            $info['list'][$k]['distance'] = round(getDistance($v['longitude'],$v['latitude'],(float)$longitude,(float)$latitude),1);
        }
        if(!empty($member_id)){
            $goods_history = Db::name('goods_history')->where('goods_id',$id)->where('member_id',$member_id)->find();
            if(empty($goods_history)){
                Db::name('goods_history')->insert(['goods_id' => $id,'create_time' => date('Y-m-d',time()),'member_id' => $member_id]);
            }

        }
        $info['evaluate_count'] = Db::name('order_evaluate')->where('single_id', $id)->where('auding_status',2)->count();
        $info['list_evaluate'] = Db::name('order_evaluate oe')
            ->join('member m', 'oe.member_id = m.id','left')
            ->field('oe.*,DATE_FORMAT(oe.create_time,"%Y-%m-%d") create_time,oe.pic_url,m.username,m.nickname,m.avatar head_pic')
            ->where('auding_status',2)
            ->where('single_id', $id)->paginate(10);
        $cart_count = Db::name('cart')->where('member_id',$member_id)->where('type','cart')->count();//购物车商品数量，跟本接口没直接关系
        return ['code' => 200,'message' => '商品详情','data' => $info,'cart_count' => $cart_count];
    }

    public function evaluate_list($member_id = 0,$id,$shop_id = 0){
        $info['evaluate_count'] = Db::name('order_evaluate')->where('single_id', $id)->where('auding_status',2)->count();
        $info['list'] = Db::name('order_evaluate oe')
            ->join('member m', 'oe.member_id = m.id','left')
            ->field('oe.*,DATE_FORMAT(oe.create_time,"%Y-%m-%d") create_time,oe.pic_url,m.username,m.nickname,m.avatar head_pic')
            ->where('auding_status',2)
            ->where('single_id', $id)->paginate(10);
        return ['code' => 200,'message' => '商品单独评论','data' => $info];
    }

    /**
     * @param $member_id
     * @return array
     * 浏览记录列表
     */
    public function history_index($member_id)
    {
        $data = Db::name('goods_history gh')
            ->join('shop_goods g', 'gh.goods_id = g.id')
            ->field('gh.create_time')
            ->where('gh.member_id', $member_id)
            ->group('gh.create_time')
            ->order('gh.create_time', 'desc')
            ->paginate(6)->toArray();
        foreach ($data['data'] as $k => &$v) {
            $v['goods_list'] = Db::name('goods_history gh')
                ->join('shop_goods g', 'gh.goods_id = g.id')
                ->field('g.id,g.title,g.price,g.picUrl')
                ->where('gh.member_id', $member_id)
                ->where('gh.create_time', $v['create_time'])
                ->select();
        }
        return ['code' => 200, 'message' => '商品历史记录', 'data' => $data];
    }

    /**
     * @param $member_id
     * @param $goods_id
     * @return array
     * 浏览记录删除
     */
    public function delete_goods_history($member_id,$goods_id){
        $ret = Db::name('goods_history')->where(['member_id' => $member_id,'goods_id' => $goods_id])->delete();
        if($ret){
            return ['code' =>200,'message' => '删除成功'];
        }
        return ['code' =>400,'message' => '失败'];
    }

    /**
     * @param $member_id
     * @return array
     * 删除所有记录
     */
    public function delete_all($member_id){
        $ret = Db::name('goods_history')->where(['member_id' => $member_id])->delete();
        if($ret){
            return ['code' =>200,'message' => '删除全部浏览记录成功'];
        }
        return ['code' =>400,'message' => '失败'];
    }

    public function attr_list($goods_id,$shop_id,$type = 'cart')
    {

        $attr_list = Db::name('shop_goods a')
            ->where('a.id',$goods_id)
            ->where('b.shop_id',$shop_id)
            ->join('shop_spec_attr b','a.id = b.goods_id')
            ->field('a.id,a.price,b.inventory,a.goods_attr')
            ->find();
        //$attr_list['goods_attr'] = unserialize($attr_list['goods_attr']);
         $attr_list['goods_attr'] = arrayChange(unserialize($attr_list['goods_attr']),'title','list');
//        foreach ($attr_list['goods_attr'] as $k => $v){
//            echo $k.'<br/>';
//            foreach ($v as $k1 => $v1){
//                echo $v1.'-';
//            }
//        }
        $spec = Db::name('shop_spec_attr')->where('goods_id',$goods_id)->where('shop_id',$shop_id)->select()->toArray();
        $list = [];
        $count = count(explode(',',$spec[0]['attribute']));

            //$count = count(explode(',',$v['attribute']));
            for ($i=0;$i<$count;$i++){
                foreach ($spec as $k => &$v){
                $list[$i]['list'][] = explode(',',$v['attribute'])[$i];
                }
            }
            //$v['attribute'] = explode(',',$v['attribute']);
        foreach ($list as $k1 => &$v1){
           $v1['list'] = array_unique($v1['list']);
        }
        foreach ($attr_list['goods_attr'] as $k2 => &$v2){
            $v2['list'] = $list[$k2]['list'];
        }


        //dump($spec);

        return ['code' => 200,'message' => '商品详情属性值','data' => $attr_list,'shop_id' => $shop_id,'type' => $type];

    }

    public function web($id,$field)
    {
        $data = Db::name('shop_goods')->where('id',$id)->value($field);
        return response($data);
    }




}